<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 17/2/4
 * Time: 下午5:57
 */

namespace App\Http\Middleware;

use Closure;

class PamMiddleware
{
    /**
     * api接口参数签名验证
     * @param $request
     * @param Closure $next
     * @return string
     */
    public function handle($request,  Closure $next)
    {
        //本地环境关闭签名
        if(env('APP_ENV') == 'local')
        {
            $arr = array_filter($request->all());
            $request->device  = isset($arr['device']) ? $arr['device'] : 'android';
            $request->version = isset($arr['version']) ? $arr['version'] : 'current';
            return $next($request);
        }

        //version check
        $r = self::_checkVersion($request);
        if($r) return $r;

        //对外访问开启签名验证
        $arr = array_filter($request->all());
        if(isset($arr['token'])) unset($arr['token']);
        ksort($arr);
        $str = '';
        if($arr)
        {
            foreach($arr as $key => $val)
            {
                $str .= $key.'='.$val.'&';
            }
        }
//        echo strtoupper(md5($str.env('APP_KEY')));
//        echo '<br>';
//        echo $request->all()['token'];

        if(isset($request->all()['token']) && strtoupper(md5($str.env('APP_KEY'))) === $request->all()['token'])
        {
            //接口时效验证 默认5分钟内有效
            if(!isset($arr['rtime']) || !$arr['rtime']) return response()->json(['status' => 500, 'msg' => config('lang_'.$request->lang.'.access_forbidden')]);
            if((time() - $arr['rtime']) > intval(env('API_TIME', 5)) * 60) return response()->json(['status' => 500, 'msg' => config('lang_'.$request->lang.'.access_forbidden')]);
            $request->version = $arr['version'];
            $request->device  = $arr['device'];
            return $next($request);
        } else {
            return response()->json(['status' => 500, 'msg' => config('lang_'.$request->lang.'.access_forbidden')]);
        }
    }


    /**
     * --------------------------
     * 终端及版本号控制
     * --------------------------
     * @param $request
     * @return \Illuminate\Http\JsonResponse
     */
    private function _checkVersion($request)
    {
        $all = $request->all();
        $device  = isset($all['device']) ? $all['device'] : '';
        $version = isset($all['version']) ? $all['version'] : '';
        switch(strtolower($device)){
            case 'android':
                if(!in_array($version, config('app.version.android')))
                {
                    return response()->json(['status' => 500, 'msg' => config('lang_'.$request->lang.'.version_update')]);
                }
                break;
            case 'ios':
                if(!in_array($version, config('app.version.ios')))
                {
                    return response()->json(['status' => 500, 'msg' => config('lang_'.$request->lang.'.version_update')]);
                }
                break;
            default:
                return response()->json(['status' => 500, 'msg' => config('lang_'.$request->lang.'.access_forbidden')]);
                break;
        }
    }
}